package com.rs.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import net.coobird.thumbnailator.Thumbnails;

public class DownloadAction {
	private InputStream inputStream;
	private String contentType;
	private String contentDisposition;
	private long contentLength;

	private String name;
	private String type;
	private int compress;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getCompress() {
		return compress;
	}

	public void setCompress(int compress) {
		this.compress = compress;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public long getContentLength() {
		return contentLength;
	}

	public void setContentLength(long contentLength) {
		this.contentLength = contentLength;
	}

	@Override
	public String toString() {
		return "DownloadAction [inputStream=" + inputStream + ", contentType=" + contentType + ", contentDisposition="
				+ contentDisposition + ", contentLength=" + contentLength + "]";
	}

	public String download() throws IOException {
		contentType = "application/octet- stream";
		String fileName;
		if (name != null) {
			ServletContext context = ServletActionContext.getServletContext();
			try {
				String path = context.getRealPath("/" + type + "/");
				System.out.println(path);
				File file = new File(path + name);
				fileName = file.getName();
				System.out.println(fileName + " downloading...");
				if (compress == 1) {
					inputStream = new FileInputStream(file);
				} else {
					try {
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						Thumbnails.of(path + name).scale(0.25f).toOutputStream(out);
						inputStream = new ByteArrayInputStream(out.toByteArray());
					} catch (Exception e) {
						inputStream = new FileInputStream(file);
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
				fileName = "1.txt";
			}
		} else {
			inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
			fileName = "1.txt";
		}
		contentLength = inputStream.available();
		contentDisposition = "attachment;filename=" + URLEncoder.encode(fileName, "utf-8");
		return "download";
	}

}
